package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"字典列表权限","dept:list"},logical = Logical.OR)
    public String list(QueryObject queryObject, Model model) {

        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.selectByQo(queryObject);

        model.addAttribute("pageInfo", pageInfo);

        return "systemDictionary/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"字典编辑或新增权限","dept:saveOrUpdate"},logical = Logical.OR)
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary, Model model) {
        JsonResult result = new JsonResult();
        try {
            systemDictionaryService.saveOrUpdate(systemDictionary);
            //保存成功
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }

}
